﻿using UnityEngine;

namespace TC
{
    /// <summary>
    /// 绘制这个节点时：特殊会绘制图里的entry和result就行
    /// </summary>
    public class CallFunc : FunctionNode
    {
        public FunctionGraph funcGraph;

        public override void OnCreate()
        {
            base.OnCreate();

            foreach (var i in funcGraph.ins)
            {
                _addInput(i.Key, i.Value);
            }

            foreach (var i in funcGraph.outs)
            {
                _addOutput(i.Key, i.Value);
            }
        }

        public override void Execute(Dispatcher chain)
        {
            base.Execute(chain);

            //外面的值输入到函数中
            foreach (var i in funcGraph.ins)
            {
                funcGraph.ins[i.Key] = (inputPorts[i.Key] as ValueInput<Variable>).Value;
            }
            funcGraph.Call(chain);
        }

        private void _addInput(string name, Variable value)
        {
            if (value is ByteVar)
            {
                this.AddValueInPort<byte>(name);
            }
            else if (value is ShortVar)
            {
                this.AddValueInPort<short>(name);
            }
            else if (value is IntVar)
            {
                this.AddValueInPort<int>(name);
            }
            else if (value is LongVar)
            {
                this.AddValueInPort<long>(name);
            }
            else if (value is FloatVar)
            {
                this.AddValueInPort<float>(name);
            }
            else if (value is CharVar)
            {
                this.AddValueInPort<char>(name);
            }
            else if (value is BoolVar)
            {
                this.AddValueInPort<bool>(name);
            }
            else if (value is VectorVar)
            {
                this.AddValueInPort<Vector3>(name);
            }
            else
            {
                this.AddValueInPort<object>(name);
            }
        }

        private void _addOutput(string name, Variable value)
        {
            if (value is ByteVar)
            {
                this.AddValueOutPort<byte>(name, () => { return funcGraph.outs[name].AsByte(); });
            }
            else if (value is ShortVar)
            {
                this.AddValueOutPort<short>(name, () => { return funcGraph.outs[name].AsShort(); });
            }
            else if (value is IntVar)
            {
                this.AddValueOutPort<int>(name, () => { return funcGraph.outs[name].AsInt(); });
            }
            else if (value is LongVar)
            {
                this.AddValueOutPort<long>(name, () => { return funcGraph.outs[name].AsLong(); });
            }
            else if (value is FloatVar)
            {
                this.AddValueOutPort<float>(name, () => { return funcGraph.outs[name].AsFloat(); });
            }
            else if (value is CharVar)
            {
                this.AddValueOutPort<char>(name, () => { return funcGraph.outs[name].AsChar(); });
            }
            else if (value is BoolVar)
            {
                this.AddValueOutPort<bool>(name, () => { return funcGraph.outs[name].AsBool(); });
            }
            else if (value is VectorVar)
            {
                this.AddValueOutPort<Vector3>(name, () => { return funcGraph.outs[name].AsVector3(); });
            }
            else
            {
                this.AddValueOutPort<object>(name, () => { return funcGraph.outs[name].AsObject<object>(); });
            }
        }
    }
}
